package com.main.common.utils.exceptions;

import lombok.extern.slf4j.Slf4j;

import java.io.PrintWriter;
import java.io.StringWriter;

@Slf4j
public class StackTraceLog {

    /**
     * 打印并输出异常的堆栈跟踪信息
     * @param e 异常
     */
    public void saveStackTraceLog(Exception e){
        log.info("StackTrace");
        // 创建一个StringWriter
        StringWriter sw = new StringWriter();
        // 将PrintWriter包装在StringWriter上
        PrintWriter pw = new PrintWriter(sw);
        // 将堆栈跟踪信息打印到PrintWriter
        e.printStackTrace(pw);
        // 获取堆栈跟踪信息的字符串表示
        String stackTrace = sw.toString();
        // 输出堆栈跟踪信息
        log.error(stackTrace);
    }

}
